package threadcoreknowledge.stopthreads.volatiledemo;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * 描述：     演示用volatile的局限part2
 * 陷入阻塞时，volatile是无法停止线程的
 * 此例中，生产者的生产速度很快，消费者消费速度慢，
 * 所以阻塞队列满了以后，生产者会阻塞，等待消费者进一步消费
 */
public class WrongWayVolatileCantStop {

    public static void main(String[] args) throws InterruptedException {
        ArrayBlockingQueue storage = new ArrayBlockingQueue(10);

        Producer producer = new Producer(storage);
        Thread producerThread = new Thread(producer,"producerThread生成者线程");
        producerThread.start();
        Thread.sleep(1000);

        Consumer consumer = new Consumer(storage);
        while (consumer.needMoreNums()) {
            System.out.println(consumer.storage.take()+"被消费了");
            Thread.sleep(100);
        }
        System.out.println("消费者不需要更多数据了.");

        //一旦消费不需要更多数据了，我们应该让生产者也停下来，但是实际情况是程序没有停下来
        producer.canceled=true;
        System.out.println("producer.canceled的状态是:"+producer.canceled);
        System.out.println("main线程结束了");

    }
}

class Producer implements Runnable {

    public volatile boolean canceled = false;
    BlockingQueue storage;

    public Producer(BlockingQueue storage) {
        this.storage = storage;
    }

    @Override
    public void run() {
        int num = 0;
        try {
            while (num <= 1_000_000 && !canceled) {
                if (num % 100 == 0) {
                    System.out.println("num 111 is "+num); // 可以看到生产者最后输出的语句一定是这一条
                    storage.put(num); // 最终是因为这里被阻塞了，所以无法正确停止线程
                    System.out.println("num 222 is "+num);
                    System.out.println(num + "是100的倍数,被放到仓库中了.");
                }
                num++;
            }
        } catch (InterruptedException e) {
            System.out.println("进入catch语句了.");
            e.printStackTrace();
        } finally {
            System.out.println("生产者结束运行");
        }
    }
}

class Consumer {

    BlockingQueue storage;

    public Consumer(BlockingQueue storage) {
        this.storage = storage;
    }

    public boolean needMoreNums() {
        double random = Math.random();
        if (random > 0.90) {
            System.out.println("random is "+random);
            return false;
        }
        return true;
    }
}